package org.jsola.hr.web;

import org.apache.poi.ss.usermodel.Workbook;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.AttendanceMonthlyAddDTO;
import org.jsola.hr.dto.AttendanceMonthlyUpdateDTO;
import org.jsola.hr.query.AttendanceMonthlyQuery;
import org.jsola.hr.service.IAttendanceMonthlyService;
import org.jsola.hr.vo.AttendanceMonthlyListVO;
import org.jsola.hr.vo.AttendanceMonthlyVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.List;


/**
 * @author zhr
 */
@RestController("hrAttendanceMonthlyController")
@RequestMapping(HrConstants.API_PATH + "/v1/attendanceMonthlies")
public class AttendanceMonthlyController implements IAttendanceMonthlyControllerAPI {

    @Autowired
    private IAttendanceMonthlyService attendanceMonthlyService;

    @Override
    @PostMapping
    public Result<AttendanceMonthlyVO> save(@Valid @RequestBody AttendanceMonthlyAddDTO attendanceMonthlyAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工考勤月统计权限校验

        return Result.success(
                attendanceMonthlyService.save(attendanceMonthlyAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody AttendanceMonthlyUpdateDTO attendanceMonthlyUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工考勤月统计权限校验

        int count = attendanceMonthlyService.update(attendanceMonthlyUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{attendanceMonthlyId}")
    public Result<Integer> delete(@PathVariable String attendanceMonthlyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工考勤月统计权限校验

        int count = attendanceMonthlyService.deleteByIds(tokenUser, attendanceMonthlyId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] attendanceMonthlyIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工考勤月统计权限校验

        int count = attendanceMonthlyService.deleteByIds(tokenUser, attendanceMonthlyIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{attendanceMonthlyId}")
    public Result<AttendanceMonthlyVO> findById(@PathVariable String attendanceMonthlyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工考勤月统计权限校验

        return Result.success(
            attendanceMonthlyService.selectById(attendanceMonthlyId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<AttendanceMonthlyListVO>> listAll(@Valid AttendanceMonthlyQuery attendanceMonthlyQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工考勤月统计权限校验

        return Result.success(
            attendanceMonthlyService.select(attendanceMonthlyQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<AttendanceMonthlyListVO>> page(@Valid AttendanceMonthlyQuery attendanceMonthlyQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            attendanceMonthlyService.selectPage(attendanceMonthlyQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping(value = "/getAttendanceMonthlyReportByPage")
    public Result<Page<AttendanceMonthlyVO>> getAttendanceMonthlyReportByPage(@Valid AttendanceMonthlyQuery attendanceMonthlyQuery){
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                attendanceMonthlyService.getAttendanceMonthlyReportByPage(attendanceMonthlyQuery,tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/downloadAttendanceMonthlyReport")
    public  void downloadAttendanceMonthlyReport(@Valid AttendanceMonthlyQuery attendanceMonthlyQuery, HttpServletResponse response) throws UnsupportedEncodingException {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验
        Workbook workbook= attendanceMonthlyService.downloadAttendanceMonthlyReport(attendanceMonthlyQuery,tokenUser.getSiteId());

        String fileName=new String(("薪资报表.xls").getBytes("GBK"), StandardCharsets.ISO_8859_1);
        response.setHeader("Content-disposition","attachment; filename="+fileName);
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        try (OutputStream outputStream = response.getOutputStream()) {
            outputStream.flush();
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    @GetMapping(value = "/getAttendanceMonthlyCount")
    public Result<Integer> getAttendanceMonthlyCount(@Valid AttendanceMonthlyQuery attendanceMonthlyQuery){
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                attendanceMonthlyService.getAttendanceMonthlyCount(attendanceMonthlyQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/getAttendanceMonthlyFileHistory")
    public Result<List<AttendanceMonthlyVO>> getAttendanceMonthlyFileHistory(@Valid AttendanceMonthlyQuery attendanceMonthlyQuery){
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                attendanceMonthlyService.getAttendanceMonthlyFileHistory(attendanceMonthlyQuery,tokenUser.getSiteId())
        );
    }

}

